package com.example.loader.view;

import com.example.loader.utils.Logger;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

/**
 * 
 * @author chenchong
 * 
 */
public class PageLoader extends ScrollView {

	protected static final String TAG = "PageLoader";
	private static int STATE = -1;
	
	private static int HEADER_HEIGHT=0;
	private static int FOOTER_HEIGHT=0;

	public PageLoader(Context context) {
		super(context);
		initView();
	}

	public PageLoader(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public PageLoader(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	/**************************************************************************/

	/**
	 * 初始化布局
	 */
	private void initView() {

	}

	/****************************************************************/
	private LoaderScrollListener listener;
	private float startY;
	private float currentY;

	/**
	 * 给PageLoader设置滑动监听
	 * 
	 * @param listener
	 *            要设置的滑动监听
	 */
	public void setOnScrollListener(LoaderScrollListener listener) {
		this.listener = listener;
	}

	/****************************************************************/

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		int action = ev.getAction();
		Logger.d("event", action + "");
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			startY = ev.getY();
			break;
		case MotionEvent.ACTION_MOVE:
			currentY = ev.getY();
			float dy = Math.abs(currentY-startY);
			if (getScrollY() <= 0) {
				// 滑动到顶部
				STATE = 0;
				HEADER_HEIGHT=listener.setHeaderHeight(dy);
			}

			if (getScrollY() + getHeight() >= computeVerticalScrollRange()) {
				// 滑动到底部
				STATE = 1;
				FOOTER_HEIGHT=listener.setFooterHeight(dy);
				scrollToBottom();
			}
			break;
		case MotionEvent.ACTION_UP:
			/*if (STATE != -1) {
				if (STATE == 0) {
					if (getScrollY() <= 0) {
//						listener.preChapter();
					}
				} else if (STATE == 1) {

					if (getScrollY() + getHeight() >= computeVerticalScrollRange()) {
//						listener.nextChapter();
					}
				}
			}*/
			
			if(HEADER_HEIGHT>=60){
				listener.preChapter();
				HEADER_HEIGHT=0;
				
			}
			if(FOOTER_HEIGHT>=60){
				listener.nextChapter();
				FOOTER_HEIGHT=0;
				
			}
			listener.setHeaderHeight(0);
			listener.setFooterHeight(0);
			break;
		}
		return super.onTouchEvent(ev);
	}

	public void scrollToTop() {
		this.scrollTo(0, 0);
	}

	public void scrollToBottom() {
		this.scrollTo(0, computeVerticalScrollRange());
	}

	/**
	 * PageLoader的滑动监听
	 * 
	 * @author chenchong
	 * 
	 */
	public interface LoaderScrollListener {

		/**
		 * 加载上一章
		 */
		abstract void preChapter();

		/**
		 * 加载下一章
		 */
		abstract void nextChapter();
		
		abstract int setHeaderHeight(float dy);
		abstract int setFooterHeight(float dy);
	}
}
